package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;

import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    //本地注入
    @Autowired
    private JedisPool jedisPool;

    //远程注入
    @Reference //远程查找
    private SetmealService setmealService;

    @RequestMapping("/upload")
public Result upload(@RequestParam("imgFile")MultipartFile imgFile){
           //获取名字
        String originalFilename=imgFile.getOriginalFilename();
           //取最后一个点的位置
        int index = originalFilename.lastIndexOf(".");
           //对点前一位后的位置分割
        String extention = originalFilename.substring(index - 1);
           //把后缀 与UUID进行拼接
        String fileName= UUID.randomUUID().toString()+extention;
        try {
            //调用七牛云上传插件
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //调用自定义类将文件名存储到大集合中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            
        } catch (IOException e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return  new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }



    //新增套餐
    @RequestMapping("add")
    public Result add(@RequestBody Setmeal setmeal, Integer [] checkgroupIds){
        try{
          setmealService.add(setmeal,checkgroupIds);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //分页查询


    //分页查询
    //检查项查询 分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }



    //删除检查组
    @RequestMapping("delete")
    public Result delete(Integer id){
        //得到id 根据id进行删除
        try{
            setmealService.delete(id);
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    //按ID查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Setmeal checkgroup=setmealService.findById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,checkgroup);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //检查组中关联检查项id查询
    @RequestMapping("/findCheckGroupIdBySetmealId")
    public Result findCheckGroupIdBySetmealId(Integer id){


        try{
            List<Integer> checkitemIds=setmealService.findCheckGroupIdBySetmealId(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //编辑方法
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer [] checkgroupIds){
        try{
            setmealService.edit(setmeal,checkgroupIds);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

}
